import {Injectable} from '@angular/core';
import {SysParamSearchForm} from '../entity/sys-param-search-form';
import {SysParam} from '../entity/sys-param';
import {HttpClient} from '@angular/common/http';
import {BaseTreeService} from '@sb/base/core/service/base-tree.service';
import {SysParamTreeNode} from '../entity/sys-param-tree-node';
import {Observable, of} from 'rxjs';
import {baseHandleError, ReturnForm} from '@sb/base';
import {catchError} from 'rxjs/operators';
import {BaseAttachmentService} from '../../sys-attachment/service/base-attachment.service';
import {SysAttachment} from '../../sys-attachment/entity/sys-attachment';

@Injectable({
    providedIn: 'root'
})
export class SysParamService extends BaseTreeService<SysParam, SysParamSearchForm, SysParamTreeNode> implements BaseAttachmentService {
    readonly basePath = 'sys/param';

    constructor(
        private http: HttpClient
    ) {
        super(http);
    }

    /**
     * 搜索框
     * @param paramCode 文本
     */
    getNodeType(paramCode: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/getNodeType`, {
            paramCode: paramCode
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('getNodeType'))
        );
    }

    getSysCodeModuleCode(paramCode: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/getSysCodeModuleCode`, {
            paramCode: paramCode
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('getSysCodeModuleCode'))
        );
    }

    /**
     * 根据多个附件id查询
     * @param ids 主键集
     * @param params 附加参数
     */
    getAttachments(ids: string[], params?: { [key: string]: any }): Observable<ReturnForm<SysAttachment[]>> {
        if (!ids
            || ids.length === 0
            || (ids.length === 1 && (ids[0] === 'null' || ids[0] === 'undefined'))) {
            return of({success: true, message: []} as ReturnForm<SysAttachment[]>);
        }
        return this._http.post<ReturnForm<SysAttachment[]>>(`${this.url}/getAttachments`, {
            ['attachmentIds']: ids,
            paramName: params['paramName']
        }).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('getAttachments'))
        );
    }

    uploadAttachment(file: FormData, params?: { [key: string]: any }): Observable<ReturnForm<SysAttachment>> {
        file.append('paramName', params['paramName']);
        return this._http.post<ReturnForm<SysAttachment>>(`${this.url}/uploadAttachment`, file).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('uploadAttachment'))
        );
    }

    getReadAttachmentUrl(id, params?: { [key: string]: any }): string {
        return `${this.url}/readAttachment/${id}?paramName=${params['paramName']}`;
    }

    getReadAttachmentThumbUrl(id, params?: { [key: string]: any }): string {
        return `${this.url}/readAttachment/thumb/${id}?paramName=${params['paramName']}`;
    }

}
